﻿/******************************************************/
// BOLTrading 事業運営クラス  
/******************************************************/

using System;
using System.Data;
using System.Data.SqlClient;
using log4net;
using MAA.Data;
using MAA.Entity;

namespace MAA.Business
{
    /// <summary>
    /// トランザクションを管理するために参照の事業運営を行うこと
    /// </summary>
    public class BOLTrading
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 検索の記事は、検索条件に従う 
        /// </summary>
        /// <param name="sortType"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="riceID"></param>
        /// <param name="provinceID"></param>
        /// <param name="priceMin"></param>
        /// <param name="priceMax"></param>
        /// <param name="tblPost"></param>
        /// <param name="rowNum"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool searchPost(
            string lang,
            string sortType,
            int pageIndex,
            int pageSize,
            int riceID,
            short provinceID,
            int priceMin,
            int priceMax,
            out DataTable tblPost,
            out long rowNum,
            out string message)
        {
            rowNum = 0;
            message = "";
            tblPost = null;
            DALPost dalPost = new DALPost();

            try
            {
                log.Info("search post");

                /// すべてのトレーディングポストは、検索条件を以下のようになる
                tblPost = dalPost.getPost(lang,sortType, pageIndex, pageSize, riceID, provinceID, priceMin, priceMax, out rowNum);
                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 現在のユーザーのすべての投稿を見る 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="userID"></param>
        /// <param name="riceID"></param>
        /// <param name="provinceID"></param>
        /// <param name="statusID"></param>
        /// <param name="tblTransactionPost"></param>
        /// <param name="numRow"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewTransactionPost(
            string lang,
            int pageIndex,
            int pageSize,
            long userID,
            int riceID,
            short provinceID,
            short statusID,
            out DataTable tblTransactionPost,
            out long numRow,
            out string message)
        {
            message = "";
            tblTransactionPost = new DataTable();
            numRow = 0;
            try
            {
                log.Info("view transaction post");
                DataColumn column;
                short status;
                // Get post by userID
                DALPost dalPost = new DALPost();
                tblTransactionPost = dalPost.getPost(lang,pageIndex, pageSize, userID, riceID, provinceID, statusID, out numRow);

                if (tblTransactionPost.Rows.Count > 0)
                {
                    // Them cot Status
                    column = new DataColumn();
                    column.ColumnName = "Status";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblTransactionPost.Columns.Add(column);

                    // Lay status theo statusID
                    status = 0;
                    for (int i = 0; i < tblTransactionPost.Rows.Count; i++)
                    {
                        status = Convert.ToInt16(tblTransactionPost.Rows[i]["StatusID"]);
                        tblTransactionPost.Rows[i]["Status"] = BOLCommon.getPostStatus(status);
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 現在のユーザーのすべての注文を表示する 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="userID"></param>
        /// <param name="riceID"></param>
        /// <param name="provinceID"></param>
        /// <param name="statusID"></param>
        /// <param name="tblTransactionOrder"></param>
        /// <param name="numRow"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewTransactionOrder(
            string lang,
            int pageIndex,
            int pageSize,
            long userID,
            int riceID,
            short provinceID,
            short statusID,
            out DataTable tblTransactionOrder,
            out long numRow,
            out string message)
        {
            message = "";
            tblTransactionOrder = new DataTable();
            numRow = 0;
            try
            {
                log.Info("view transaction order");
                DataColumn column;
                short status;
                // Get post by userID
                DALOrder dalOrder = new DALOrder();
                tblTransactionOrder = dalOrder.getOrder(lang, pageIndex, pageSize, userID, riceID, provinceID, statusID, out numRow);

                if (tblTransactionOrder.Rows.Count > 0)
                {
                    // Them cot Status
                    column = new DataColumn();
                    column.ColumnName = "Status";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblTransactionOrder.Columns.Add(column);

                    // Lay status theo statusID
                    status = 0;
                    for (int i = 0; i < tblTransactionOrder.Rows.Count; i++)
                    {
                        status = Convert.ToInt16(tblTransactionOrder.Rows[i]["StatusID"]);
                        tblTransactionOrder.Rows[i]["Status"] = BOLCommon.getOrderStatus(status);
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 投稿を作成 
        /// </summary>
        /// <param name="post"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool createPost(Post post, out string message)
        {
            message = "";
            try
            {
                log.Info("create post");
                DALPost dalPost = new DALPost();
                DALWarehouse dalWh = new DALWarehouse();
                DALRice dalRice = new DALRice();
                // Check amount
                double amountWh = 0;
                // Lay so luong gao trong kho
                DataTable tblWhAmount = dalWh.getWhRiceAmount(post.UserID, post.RiceID);
                if (tblWhAmount != null && tblWhAmount.Rows.Count > 0)
                {
                    for (int i = 0; i < tblWhAmount.Rows.Count; i++)
                    {
                        amountWh = Convert.ToDouble(tblWhAmount.Rows[i]["Amount"]);
                    }
                }
                if (post.Amount > amountWh)
                {
                    message = Common.MESSAGE_ERROR_INVALID_POST_AMOUNT;
                    return false;
                }

                //Check price
                int currentPrice = 0;
                short validPercentPrice = 0;
                // Lay price va percent price from database
                DataTable tblPrice = dalRice.getRicePrice(post.RiceID);
                if (tblPrice != null && tblPrice.Rows.Count > 0)
                {
                    for (int i = 0; i < tblPrice.Rows.Count; i++)
                    {
                        currentPrice = Convert.ToInt32(tblPrice.Rows[i]["CurrentPrice"]);
                        validPercentPrice = Convert.ToInt16(tblPrice.Rows[i]["PercentPrice"]);
                    }
                }
                double priceMarketMax = currentPrice + (currentPrice * validPercentPrice / 100);
                double priceMarketMin = currentPrice - (currentPrice * validPercentPrice / 100);
                if (((double)post.Price > priceMarketMax) || ((double)post.Price < priceMarketMin))
                {
                    message = Common.MESSAGE_ERROR_INVALID_POST_PRICE;
                    return false;
                }

                // Insert a post in database
                post.Date = DateTime.Today;
                post.Status = 1;
                dalPost.insertPost(post);

                // Reduce rice amount of rice type of the post in warehouse
                dalWh.reduceWhRice(post.UserID, post.RiceID, post.Amount);
                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// ポストの詳細を参照して情報 
        /// </summary>
        /// <param name="postID"></param>
        /// <param name="tblPost"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewPost(string lang, long postID, out DataTable tblPost, out string message)
        {
            message = "";
            tblPost = null;

            DALPost dalPost = new DALPost();
            try
            {
                log.Info("view info");

                // Get detail information of post
                tblPost = dalPost.getPostInfo(lang,postID);

                if (tblPost.Rows.Count > 0)
                {
                    // Them cot Status cua Post
                    DataColumn column = new DataColumn();
                    column.ColumnName = "StatusName";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblPost.Columns.Add(column);

                    short status = 0;
                    for (int i = 0; i < tblPost.Rows.Count; ++i)
                    {
                        status = Convert.ToInt16(tblPost.Rows[i]["Status"]);
                        tblPost.Rows[i]["StatusName"] = BOLCommon.getPostStatus(status);
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// ポストのビューバイヤー＆オーダーリスト 
        /// </summary>
        /// <param name="postID"></param>
        /// <param name="tblBuyerOrder"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewOrderList(string lang, long postID, out DataTable tblBuyerOrder, out string message)
        {
            message = "";
            tblBuyerOrder = null;

            DALOrder dalOrderPost = new DALOrder();
            try
            {
                log.Info("view order list");

                // Get information of buyers & orders
                tblBuyerOrder = dalOrderPost.getOrderPost(lang, postID);

                if (tblBuyerOrder.Rows.Count > 0)
                {
                    // Them cot Status cua Order
                    DataColumn column = new DataColumn();
                    column.ColumnName = "StatusName";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblBuyerOrder.Columns.Add(column);

                    short status = 0;
                    for (int i = 0; i < tblBuyerOrder.Rows.Count; ++i)
                    {
                        status = Convert.ToInt16(tblBuyerOrder.Rows[i]["Status"]);
                        tblBuyerOrder.Rows[i]["StatusName"] = BOLCommon.getOrderStatus(status);
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// ポストのトランザクションをキャンセル 
        /// </summary>
        /// <param name="post"></param>
        /// <param name="orderList"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool cancelPost(string lang, Post post, out string message)
        {
            message = "";

            DALPost dalPost = new DALPost();
            DALOrder dalOrder = new DALOrder();
            DALWarehouse dALWarehouse = new DALWarehouse();
            int numPendingOrder = 0;
            int numAcceptOrder = 0;
            int numRejectOrder = 0;

            try
            {
                log.Info("cancel post");

                // Check Order list
                DataTable tblBuyerOrder = dalOrder.getOrderPost(lang, post.ID);
                if (tblBuyerOrder.Rows.Count > 0)
                {
                    for (int i = 0; i < tblBuyerOrder.Rows.Count; ++i)
                    {
                        if (Convert.ToInt16(tblBuyerOrder.Rows[i]["Status"]) == 1)
                        {
                            numPendingOrder += 1;
                        }
                        if (Convert.ToInt16(tblBuyerOrder.Rows[i]["Status"]) == 2)
                        {
                            numAcceptOrder += 1;
                        }
                        if (Convert.ToInt16(tblBuyerOrder.Rows[i]["Status"]) == 3)
                        {
                            numRejectOrder += 1;
                        }
                    }
                }

                // If number of processing order = 0
                if (numPendingOrder == 0)
                {
                    // Return rice to warehouse
                    long returnAmount = post.Amount - post.TradedAmount;
                    dALWarehouse.raiseWhRice(post.UserID, post.RiceID, returnAmount);

                    // If number of accepted order > 0
                    if (numAcceptOrder > 0)
                    {
                        // Update status of Post is success
                        short postStatus = 2;
                        dalPost.updatePost(post.ID, postStatus);
                    }
                    // If number of accepted order = 0
                    else if (numAcceptOrder == 0)
                    {
                        // If number of rejected order > 0 --> delete rejected orders
                        if (numRejectOrder > 0)
                        {
                            dalOrder.deleteOrder(post.ID, 3);
                        }

                        // Delete post
                        dalPost.deletePost(post.ID);
                    }
                    return true;
                }
                else
                {
                    message = Common.MESSAGE_ERROR_DELETE_POST_FAIL;
                    return false;
                }
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }

        }

        /// <summary>
        /// 注文を作成 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="postChoice"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool createOrder(string lang,Order order, out string message)
        {
            message = "";

            DALOrder dalOrder = new DALOrder();
            DALPost dalPost = new DALPost();
            Post postChoice = new Post();

            try
            {
                log.Info("create order");
                // Check Post
                DataTable tblPost = dalPost.getPostInfo(lang,order.PostID);
                if (tblPost.Rows.Count > 0)
                {
                    for (int i = 0; i < tblPost.Rows.Count; ++i)
                    {
                        postChoice.Status = Convert.ToInt16(tblPost.Rows[i]["Status"]);
                        postChoice.Amount = Convert.ToInt64(tblPost.Rows[i]["Amount"]);
                        postChoice.TradedAmount = Convert.ToInt64(tblPost.Rows[i]["TradedAmount"]);
                        postChoice.MinOrderAmount = Convert.ToInt32(tblPost.Rows[i]["MinOrderAmount"]);
                        postChoice.RiceID = Convert.ToInt32(tblPost.Rows[i]["RiceID"]);
                        postChoice.Price = Convert.ToInt32(tblPost.Rows[i]["Price"]);
                        postChoice.PercentPrice = Convert.ToInt16(tblPost.Rows[i]["PercentPrice"]);
                    }
                    if (postChoice.Status != 1)
                    {
                        message = Common.MESSAGE_ERROR_CREATE_ORDER_FAIL;
                        return false;
                    }
                }
                else
                {
                    message = Common.MESSAGE_ERROR_POST_NOT_EXIST;
                    return false;
                }

                // Get valid maximum & minimum amount of Order
                long maxOrderAmount = postChoice.Amount - postChoice.TradedAmount;
                long minOrderAmount = postChoice.MinOrderAmount;

                // Get valid maximum & minimum price of Order based on market price of Rice
                DALRice dalRice = new DALRice();
                DataTable tblRice = dalRice.getRicePrice(postChoice.RiceID);
                double validMarketPerPrice = 0;
                double maxMarketPrice = 0;
                double minMarketPrice = 0;

                if (tblRice.Rows.Count > 0)
                {
                    int riceCurrentPrice = 0;
                    short ricePercentPrice = 0;
                    for (int i = 0; i < tblRice.Rows.Count; ++i)
                    {
                        riceCurrentPrice = Convert.ToInt32(tblRice.Rows[i]["CurrentPrice"]);
                        ricePercentPrice = Convert.ToInt16(tblRice.Rows[i]["PercentPrice"]);
                    }

                    validMarketPerPrice = riceCurrentPrice * ricePercentPrice / 100;
                    maxMarketPrice = riceCurrentPrice + validMarketPerPrice;
                    minMarketPrice = riceCurrentPrice - validMarketPerPrice;
                }

                // Get valid maximum & minimum price of Order based on price of Post
                double validPostPerPrice = postChoice.Price * postChoice.PercentPrice / 100;
                double maxPostPrice = postChoice.Price + validPostPerPrice;
                double minPostPrice = postChoice.Price - validPostPerPrice;

                // If amount of Order is valid && price of Order is valid
                if (order.Amount <= maxOrderAmount && order.Amount >= minOrderAmount)
                {
                    if (((double)order.Price <= maxMarketPrice && (double)order.Price >= minMarketPrice) &&
                        ((double)order.Price <= maxPostPrice && (double)order.Price >= minPostPrice))
                    {
                        // Create order
                        order.Date = DateTime.UtcNow.Date;
                        order.Status = 1;
                        dalOrder.insertOrder(order);
                        return true;
                    }
                    else
                    {
                        message = Common.MESSAGE_ERROR_INVALID_ORDER_PRICE;
                        return false;
                    }
                }
                else
                {
                    message = Common.MESSAGE_ERROR_INVALID_ORDER_AMOUNT;
                    return false;
                }
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// オーダー＆ポスト情報の詳細を参照してください情報は、その順序に対応 
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="tblOrder"></param>
        /// <param name="tblPostOrder"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewOrder(string lang, long orderID, out DataTable tblOrder, out DataTable tblPostOrder, out string message)
        {
            message = "";
            tblOrder = null;
            tblPostOrder = null;

            DALOrder dalOrder = new DALOrder();
            DALPost dalPost = new DALPost();
            try
            {
                log.Info("view order");

                // Get detail information of order
                tblOrder = dalOrder.getOrderInfo(orderID);

                if (tblOrder.Rows.Count > 0)
                {
                    // Them cot Status cua Order
                    DataColumn column = new DataColumn();
                    column.ColumnName = "StatusName";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblOrder.Columns.Add(column);

                    short status = 0;
                    for (int i = 0; i < tblOrder.Rows.Count; ++i)
                    {
                        status = Convert.ToInt16(tblOrder.Rows[i]["Status"]);
                        tblOrder.Rows[i]["StatusName"] = BOLCommon.getOrderStatus(status);
                    }
                }

                // Get information of post correspond to that order
                tblPostOrder = dalPost.getPostOrder(lang, orderID);

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 注文を取り消す 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool cancelOrder(long orderID, out string message)
        {
            message = "";

            DALOrder dalOrder = new DALOrder();
            short orderStatus = 0;

            try
            {
                log.Info("cancel order");

                // Check Order
                DataTable tblOrderCheck = dalOrder.getOrderInfo(orderID);
                if (tblOrderCheck.Rows.Count > 0)
                {
                    for (int i = 0; i < tblOrderCheck.Rows.Count; ++i)
                    {
                        orderStatus = Convert.ToInt16(tblOrderCheck.Rows[i]["Status"]);
                    }
                }
                else
                {
                    return true;
                }

                // If status of Order is processing --> perform delete Order
                if (orderStatus == 1)
                {
                    dalOrder.deleteOrder(orderID);
                    return true;
                }
                else
                {
                    message = Common.MESSAGE_ERROR_DELETE_ORDER_FAIL;
                    return false;
                }
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 順序を受け入れるか拒否 
        /// </summary>
        /// <param name="post"></param>
        /// <param name="orderChoice"></param>
        /// <param name="processType"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool processTransaction(Post post, long orderChoiceID, bool processType, out string message)
        {
            message = "";

            DALOrder dalOrder = new DALOrder();
            DALPost dalPost = new DALPost();
            BOLCommon bolCommon = new BOLCommon();
            Order orderChoice = new Order();

            try
            {
                log.Info("process Transaction");
                // Check Order
                DataTable tblOrder = dalOrder.getOrderInfo(orderChoiceID);

                if (tblOrder.Rows.Count > 0)
                {
                    for (int i = 0; i < tblOrder.Rows.Count; ++i)
                    {
                        orderChoice.Amount = Convert.ToInt64(tblOrder.Rows[i]["Amount"]);
                        orderChoice.Price = Convert.ToInt32(tblOrder.Rows[i]["Price"]);
                    }
                }
                else
                {
                    message = Common.MESSAGE_ERROR_ORDER_NOT_EXIST;
                    return false;
                }

                // Case accept Order
                if (processType == true)
                {
                    // Check amount of Order
                    long maxOrderAmount = post.Amount - post.TradedAmount;

                    // If amount of Order is valid
                    if (orderChoice.Amount <= maxOrderAmount)
                    {
                        // Update status of Order is success
                        dalOrder.updateOrder(orderChoiceID, 2);

                        // Update traded amount of Post
                        long tradedAmount = post.TradedAmount + orderChoice.Amount;
                        dalPost.updatePost(post.ID, tradedAmount);

                        // If traded amount sau khi accept Order = total amount of Post 
                        if (tradedAmount == post.Amount)
                        {
                            // Update status of Post is success
                            short postStatus = 2;
                            dalPost.updatePost(post.ID, postStatus);
                        }
                    }
                    // If amount of Order is invalid
                    else
                    {
                        message = Common.MESSAGE_ERROR_NOT_ENOUGH_POST_AMOUNT;
                        return false;
                    }
                }
                // Case reject Order
                else
                {
                    // Update status of Order is fail
                    dalOrder.updateOrder(orderChoiceID, 3);
                }
                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 現在のポストの販売業者にコンタクトメッセージを送る
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="receiverID"></param>
        /// <param name="content"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool createContact(long userID, long receiverID, string content, out string message)
        {
            // Send email to Receiver noi dung tin nhan cua User
            string userName;
            string userEmail;
            BOLCommon.getUserInfoForEmail(userID, out userName, out userEmail, out message);

            string receiverName;
            string receiverEmail;
            BOLCommon.getUserInfoForEmail(receiverID, out receiverName, out receiverEmail, out message);

            string bodyMail = string.Format(
                "Xin chào " + receiverName + ",<br/>" +
                userName + " đã gửi bạn một tin nhắn:<br/>" +
                content);

            bool sendEmail = BOLCommon.SendMailMulti(
                Common.SEND_EMAIL_SERVER,
                Common.SEND_EMAIL_USER,
                Common.SEND_EMAIL_PASSWORD,
                Common.SEND_EMAIL_PORT,
                Common.SEND_EMAIL_DISPLAY_NAME,
                Common.SEND_EMAIL_FROM,
                receiverEmail,
                userEmail,
                "",
                Common.SEND_EMAIL_SUBJECT + userName + " đã gửi bạn một tin nhắn",
                bodyMail,
                true,
                true);

            if (sendEmail == true)
            {
                message = Common.MESSAGE_SUCCESS_CONTACT;
                return true;
            }
            else
            {
                message = Common.MESSAGE_ERROR_SEND_EMAIL_FAIL;
                return false;
            }
        }
    }
}